package com.lingchen.boot.utils;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

@SuppressWarnings("all")
public class FileUtils {  
      
    public static boolean IsExist(String path){
        boolean Is;  
        File file=new File(path);  
        Is = file.exists();  
        return Is;  
    }  
      
    /** 
     * @方法名称 :wrieteFile 
     * @功能描述 :写文件操作 
     * @返回值类型 :void 
     * @param path 文件路径 ，content 写的内容 
     */  
    public static void wrieteFile(String path, String content){  
  
        if(IsExist(path)) {  
            OutputStream out;  
            try {  
                out = new BufferedOutputStream(new FileOutputStream(path));  
                byte[] time = content.getBytes();  
                out.write(time);  
                out.close();  
            } catch (FileNotFoundException e) {  
                e.printStackTrace();  
            } catch (IOException e) {  
                e.printStackTrace();  
  
            }  
        }  
    }  
      
    /** 
     * @方法名称 :readLineFile 
     * @功能描述 :一行一行读文件内容 
     * @返回值类型 :void 
     * @param filename 文件名 
     */  
    public static List<String> readLineFile(String filename){
        List<String> list = new ArrayList<>();
        try {  
            FileInputStream in = new FileInputStream(filename);  
            InputStreamReader inReader = new InputStreamReader(in, "UTF-8");  
            BufferedReader bufReader = new BufferedReader(inReader);  
            String line = null;  
            int i = 1;  
            while((line = bufReader.readLine()) != null){  
               list.add(line);
                i++;  
            }  
            bufReader.close();  
            inReader.close();  
            in.close();  
        } catch (Exception e) {  
            e.printStackTrace();  
            System.out.println("读取" + filename + "出错！");  
        }
        return list;
    }  
      
    /** 
     * @方法名称 :writeLineFile 
     * @功能描述 :一行一行写文件 
     * @返回值类型 :void 
     * @param filename 文件名，content 写的内容 
     */  
    public static void writeLineFile(String filename, String[] content){  
  
        if(IsExist(filename)) {  
            try {  
                FileOutputStream out = new FileOutputStream(filename);  
                OutputStreamWriter outWriter = new OutputStreamWriter(out, "UTF-8");  
                BufferedWriter bufWrite = new BufferedWriter(outWriter);  
                for (int i = 0; i < content.length; i++) {  
                    bufWrite.write(content[i] + "\r\n");  
                }  
                bufWrite.close();  
                outWriter.close();  
                out.close();  
            } catch (Exception e) {  
                e.printStackTrace();  
                System.out.println("读取" + filename + "出错！");  
            }         
        }  
    }  
      
    /** 
     * @方法名称 :writeLineFile 
     * @功能描述 :一行一行写文件 
     * @返回值类型 :void 
     * @param filename 文件名，content 写的内容 
     */  
    public static void writeLineFile(String filename, String content){  
        try {  
            FileOutputStream out = new FileOutputStream(filename, true);  
            OutputStreamWriter outWriter = new OutputStreamWriter(out, "UTF-8");  
            BufferedWriter bufWrite = new BufferedWriter(outWriter);  
            bufWrite.write(content + "\r\n");  
            bufWrite.close();  
            outWriter.close();  
            out.close();  
        } catch (Exception e) {  
            e.printStackTrace();  
            System.out.println("读取" + filename + "出错！");  
        }         
    }  
}  